package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.infantschool.R;
import com.android.infantschool.ui.imageloader.Media;
import com.android.infantschool.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import tcking.github.com.giraffeplayer.GiraffePlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\12 0012 17:41
 * @class describe
 */
public class VideoActivity extends BaseActivity {

   /*@BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.videoView)
    public VideoView videoView;

    private Media mediaController;*/

    private GiraffePlayer player;

    public static Intent newIntent(Activity activity, String videoUrl) {
        Intent intent = new Intent(activity, VideoActivity.class);
        intent.putExtra("videoUrl", videoUrl);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        //playVideo();
        player = new GiraffePlayer(this);
        player.play(getIntent().getStringExtra("videoUrl"));

        player.onComplete(new Runnable() {
            @Override
            public void run() {
                //callback when video is finish
                Log.e(TAG, "播放完成");
            }
        }).onInfo(new GiraffePlayer.OnInfoListener() {
            @Override
            public void onInfo(int what, int extra) {
                switch (what) {
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
                        //do something when buffering start
                        break;
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
                        //do something when buffering end
                        break;
                    case IMediaPlayer.MEDIA_INFO_NETWORK_BANDWIDTH:
                        //download speed
                        ((TextView) findViewById(R.id.tv_speed)).setText(Formatter.formatFileSize(getApplicationContext(), extra) + "/s");
                        break;
                    case IMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
                        //do something when video rendering
                        findViewById(R.id.tv_speed).setVisibility(View.GONE);
                        break;
                }
            }
        }).onError(new GiraffePlayer.OnErrorListener() {
            @Override
            public void onError(int what, int extra) {
                ToastUtil.showShortToast(VideoActivity.this, "video play error");
            }
        });
    }

    /**
     * 播放视频
     */
    /*private void playVideo() {
        mediaController = new Media(VideoActivity.this, false);
        mediaController.setMediaPlayer(videoView);
        videoView.setVideoPath(getIntent().getStringExtra("videoUrl"));
        videoView.setEnabled(false);
        videoView.setFocusable(false);
        videoView.setMediaController(mediaController);
        videoView.setOnCompletionListener(this);
        videoView.start();
        videoView.requestFocus();
    }*/

    /*@Override
    public void onCompletion(MediaPlayer mp) {
        Log.e(getClass().getSimpleName(), "播放结束");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        this.finish();
    }*/
    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (player != null) {
            player.onResume();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (player != null) {
            player.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onBackPressed() {
        if (player != null && player.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.onDestroy();
        }
    }
}